package cn.heiliu.selenium;

import cn.heiliu.selenium.config.SetupConfig;
import cn.heiliu.selenium.service.ISeleniumPoolService;
import cn.heiliu.selenium.service.IWebDriverCreateService;
import cn.heiliu.selenium.service.IWebDriverService;
import cn.heiliu.selenium.service.impl.FirefoxWebDriverCreateServiceImpl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wangyelou
 * @date 2021/6/14
 */
public class GetWebDriverFromPool {

    public static void main(String[] args) {

        SetupConfig setupConfig = SetupConfig.builder().binPath("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
                .enginePath("D:\\work\\firefox-selenium\\geckodriver.exe")
                .proxyAuthExt("D:\\work\\firefox-selenium\\firefox_proxy_auth.xpi")
                .poolSize(3).build();
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        ISeleniumPoolService seleniumPool = SeleniumManager.createFirefoxSeleniumPool(setupConfig);

        for (int i=0; i<10; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    IWebDriverService webDriver = seleniumPool.getWebDriver(3000);
                    try {
                        webDriver.fetch("http://www.badiu.com/");
                    } finally {
                        seleniumPool.release(webDriver);
                    }
                }
            });
        }


    }

}
